﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Exam.IRepositoy;

namespace Exam.Repositoy
{
    public class BaseRepository<T>:IBaseRepository<T> where T : class 
    {

        internal Exam.EntityFramework.ExamDbContext _examDbContext;
        public BaseRepository(Exam.EntityFramework.ExamDbContext examDbContext)
        {
            _examDbContext = examDbContext;
        }
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<T> Add(T t)
        {
            _examDbContext.Set<T>().Add(t);
            await _examDbContext.SaveChangesAsync();
            return t;
        }

        public async Task<T> Update(T t)
        {
            _examDbContext.Set<T>().Update(t);
            await _examDbContext.SaveChangesAsync();
            return t;
        }
        /// <summary>
        /// 硬删除
        /// </summary>
        /// <param name="ts"></param>
        /// <returns></returns>
        public async Task<int> Delete(List<T> ts)
        {
            _examDbContext.Set<T>().RemoveRange(ts);
            return await _examDbContext.SaveChangesAsync();

        }

        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="ts"></param>
        /// <returns></returns>
        public async Task<int> SoftDelete(List<T> ts)
        {
            _examDbContext.Set<T>().UpdateRange(ts);
            return await _examDbContext.SaveChangesAsync();

        }
    }
}
